package random.fox.p2p.ui;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import random.fox.p2p.App;
import random.fox.p2p.IndexActivity;
import random.fox.p2p.R;
import random.fox.p2p.event.FinishActivityEvent;
import random.fox.p2p.model.LoginModule;
import random.fox.p2p.presenter.LoginPresenter;
import random.fox.p2p.view.ILoginView;

/**
 * Created by wq on 15/3/14.
 */
public class LoginView extends FrameLayout implements ILoginView {

    @Inject
    LoginPresenter loginPresenter;

    @InjectView(R.id.login_progressbar)
    ProgressBar progressBar;

    @InjectView(R.id.txt_erro_info)
    TextView errorTextInfo;
    @InjectView(R.id.edt_name)
    EditText userNameText;
    @InjectView(R.id.edt_pwd)
    EditText pwdText;
    @InjectView(R.id.is_save_pwd)
    CheckBox checkBox;

    public LoginView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.inject(this);
        ((App) getContext().getApplicationContext()).createScopedGraph(new LoginModule(this)).inject(this);
        loginPresenter.init();
    }

    @OnClick(R.id.btn_login) void loginClicked() {
        loginPresenter.validate(userNameText.getText()+"", pwdText.getText()+"");
    }

    @Override
    public void showProgress() {
        if (progressBar.getVisibility() == GONE) {
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideProgress() {
        if (progressBar.getVisibility() == View.VISIBLE) {
            progressBar.setVisibility(View.GONE);
        }
    }

    @Override
    public void showMessage(String message) {
        errorTextInfo.setText(message);
    }

    @Override
    public void goToHome() {
        Intent intent = new Intent(getContext(), IndexActivity.class);
        getContext().startActivity(intent);
        EventBus.getDefault().post(new FinishActivityEvent());
    }

    @Override
    public boolean isSavePassword() {
        return checkBox.isChecked();
    }


    @Override
    public void fillData(String name , String password) {
        userNameText.setText(name);
        pwdText.setText(password);
    }

}
